package com.db.collections;

/**
 * @author nulls.narik
 */
public class TestArrayCopy extends TestCollections {

    public static void main(String[] args) {
        TestArrayCopy testArrayCopy = new TestArrayCopy();
        testArrayCopy.test();
    }

    @Override
    public void test() {
        for (int i = 0; i < 100; i++) {
            warmingUp(10000);
        }

        copying(1000);
        copying(100000);
        copying(10000000);
        copying(1000000000);
    }

    private void warmingUp(int n) {
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = randInt();
        }

        //прогрев
        for (int j = 0; j < n; j++) {
            int[] copyFor = new int[n];
            for (int i=0; i<n; i++) {
                copyFor[i] = array[i];
            }

            int[] copyCopy = new int[n];
            System.arraycopy(array, 0, copyCopy, 0, n);
        }
    }

    private void copying(int n) {
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = randInt();
        }

        long timeFor = 0;
        long timeCopy = 0;
        for (int i = 0; i < 100; i++) {
            int[] copyFor = new int[array.length];
            long start, end;
            start = System.nanoTime();
            for (int j = 0; j < array.length; j++) {
                copyFor[j] = array[j];
            }
            end = System.nanoTime();
            timeFor += end - start;

            int[] copyCopy = new int[array.length];

            start = System.nanoTime();
            System.arraycopy(array, 0, copyCopy, 0, array.length);
            end = System.nanoTime();
            timeCopy += end - start;
        }

        System.out.printf("%d: %f - %f. %f%n", n, timeFor / 100.0 / n, timeCopy / 100.0 / n, (double) timeFor / timeCopy);
    }
}
